package com.wave.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wave.common.core.domain.PageQuery;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.utils.StringUtils;
import com.wave.system.domain.SysProduceArrange;
import com.wave.system.domain.SysProductConfig;
import com.wave.system.domain.bo.SysProductConfigBo;
import com.wave.system.domain.vo.SysProductConfigVo;
import com.wave.system.mapper.SysProduceArrangeMapper;
import com.wave.system.mapper.SysProductConfigMapper;
import com.wave.system.service.ISysProductConfigService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 产品配置Service业务层处理
 *
 * @author wave
 * @date 2024-10-12
 */
@RequiredArgsConstructor
@Service
public class SysProductConfigServiceImpl implements ISysProductConfigService {

    private final SysProductConfigMapper baseMapper;

    private final SysProduceArrangeMapper sysProduceArrangeMapper;

    /**
     * 查询产品配置
     */
    @Override
    public SysProductConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品配置列表
     */
    @Override
    public TableDataInfo<SysProductConfigVo> queryPageList(SysProductConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysProductConfig> lqw = buildQueryWrapper(bo);
        Page<SysProductConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品配置列表
     */
    @Override
    public List<SysProductConfigVo> queryList(SysProductConfigBo bo) {
        LambdaQueryWrapper<SysProductConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysProductConfig> buildQueryWrapper(SysProductConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysProductConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProductNumber()), SysProductConfig::getProductNumber, bo.getProductNumber());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), SysProductConfig::getProductName, bo.getProductName());
        return lqw;
    }

    /**
     * 新增产品配置
     */
    @Override
    public Boolean insertByBo(SysProductConfigBo bo) {
        SysProductConfig add = BeanUtil.toBean(bo, SysProductConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品配置
     */
    @Override
    public Boolean updateByBo(SysProductConfigBo bo) {
        SysProductConfig update = BeanUtil.toBean(bo, SysProductConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysProductConfig entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean checkProductNameUnique(SysProductConfigBo bo) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysProductConfig>()
            .eq(SysProductConfig::getProductName, bo.getProductName())
            .ne(ObjectUtil.isNotNull(bo.getId()), SysProductConfig::getId, bo.getId()));
        return !exist;
    }

    @Override
    public Boolean checkProductNumberUnique(SysProductConfigBo bo) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysProductConfig>()
            .eq(SysProductConfig::getProductNumber, bo.getProductNumber())
            .ne(ObjectUtil.isNotNull(bo.getId()), SysProductConfig::getId, bo.getId()));
        return !exist;
    }

    @Override
    public Boolean checkProductHasBeUsed(Long[] ids) {
        Long id = ids[0];
        SysProductConfig sysProductConfig = baseMapper.selectById(id);
        if (ObjectUtil.isEmpty(sysProductConfig)) {
            return false;
        }
        List<SysProduceArrange> list = sysProduceArrangeMapper.selectList(Wrappers.lambdaQuery(SysProduceArrange.class)
            .eq(SysProduceArrange::getProductNumber, sysProductConfig.getProductNumber()));
        return CollectionUtils.isNotEmpty(list);
    }

}
